﻿using Report.Entity;
using Report.Helper;
using Report.IRepository;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq.Expressions;
using System.Text;

namespace Report.Repository
{
    public class PaiGongDal 
    {
        public SqlSugarClient db;
        public PaiGongDal()
        {
            db = SqlSugarHelper.GetClient();
        }
        //出库 
        public string outKuCun(string barcode,string paigonganId,String uid) {
            派工单任务 paigongBeen=null;
            if (!"".Equals(paigonganId)) {
                paigongBeen = searchByPaiGong(paigonganId);
               
            }
            在制流转条码 barcodeEntity=SearchByBarCode(barcode);
            if (barcodeEntity == null)
            {
               //条码错误
                return "产品条码不是待出库状态";
            }
            if (!"".Equals(paigonganId))
            {
                String chanpinxitongbianhao = SearchChanpinByDingdan(paigonganId);
                if (!barcodeEntity.产品系统编号.Equals(chanpinxitongbianhao)) {
                    return "产品不匹配,禁止出库";
                }
            }
            //检查仓位
            仓库仓位 posEntity = new PositionDAL().SearchByPosition("C"+barcodeEntity.仓位);
            if (posEntity == null)
            {
                return "仓位条码不存在,或者仓位在盘点";
            }
            标准仓库参数子表 cangkuzibiao=searchCangkuBybianhao(barcodeEntity.仓库编号);
            if (barcodeEntity == null) {
                //仓库不存在
                return "仓位类别不存在";
            }
            //产生
            成品出入库主表 headEntity = new 成品出入库主表();
            成品出入库子表 detailEntity = new 成品出入库子表();
            //主表
            string startWith = cangkuzibiao.类别编号 + "I";
            string no = getNextSeq(startWith);
            headEntity.仓库类别编号 = cangkuzibiao.类别编号;
            headEntity.出入类别 = "出库";
            headEntity.单据编号 = no;
            headEntity.单据日期 = DateTime.Now;
            if (!"".Equals(paigonganId))
            {
                headEntity.单据类别 = "领用出库"; //
            }
            else {
                headEntity.单据类别 = "其他出库"; //
            }
            headEntity.仓库编号 = barcodeEntity.仓库编号;
            headEntity.仓库核算类别 = cangkuzibiao.核算类别;
            headEntity.经手人 = uid;
            headEntity.创建人 = uid;
            headEntity.创建时间 = DateTime.Now;
            headEntity.审核 = true;
            headEntity.审核人 = uid;
            headEntity.审核时间 = DateTime.Now;
            headEntity.库存更新 = true;
            if (paigongBeen != null)
            {
                headEntity.任务单号 = paigongBeen.派工单号;
            }
            else {
                headEntity.任务单号 = "";
            }
           
            //子表
            detailEntity.单据编号 = no;
            detailEntity.单据序号 = no + "01";
            detailEntity.产品系统编号 = barcodeEntity.产品系统编号;
            detailEntity.颜色 = barcodeEntity.颜色;
            detailEntity.自定义1 = barcodeEntity.自定义1;
            detailEntity.自定义2 = barcodeEntity.自定义2;
            detailEntity.自定义3 = barcodeEntity.自定义3;
            detailEntity.自定义4 = barcodeEntity.自定义4;
            detailEntity.自定义5 = barcodeEntity.自定义5;
            detailEntity.自定义6 = barcodeEntity.自定义6;
            detailEntity.自定义7 = barcodeEntity.自定义7;
            detailEntity.自定义8 = barcodeEntity.自定义8;
            detailEntity.库存自定义1 = barcodeEntity.工序系统编号;
            detailEntity.库存自定义2 = "";
            detailEntity.库存自定义3 = "";
            detailEntity.库存自定义4 = "";
            detailEntity.库存自定义5 = "";
            detailEntity.条码 = barcodeEntity.条码;
            detailEntity.仓位 = barcodeEntity.仓位;
            detailEntity.数量 = barcodeEntity.数量;
          //  detailEntity.导入序号 = barcodeEntity.流程单号;
            detailEntity.排序 = 1;
            detailEntity.审核 = true;
            detailEntity.创建人 = uid;
            detailEntity.创建时间 = DateTime.Now;

            //总规格
            string tSpk = headEntity.仓库类别编号
                    + "/" + (headEntity.仓库编号 == null ? "" : headEntity.仓库编号)
                    + "/" + (headEntity.仓库核算类别 == null ? "" : headEntity.仓库核算类别)
                    + "/" + (detailEntity.产品系统编号 == null ? "" : detailEntity.产品系统编号)
                    + "/" + (detailEntity.自定义1 == null ? "" : detailEntity.自定义1)
                    + "/" + (detailEntity.自定义2 == null ? "" : detailEntity.自定义2)
                    + "/" + (detailEntity.自定义3 == null ? "" : detailEntity.自定义3)
                    + "/" + (detailEntity.自定义4 == null ? "" : detailEntity.自定义4)
                    + "/" + (detailEntity.自定义5 == null ? "" : detailEntity.自定义5)
                    + "/" + (detailEntity.自定义6 == null ? "" : detailEntity.自定义6)
                    + "/" + (detailEntity.自定义7 == null ? "" : detailEntity.自定义7)
                    + "/" + (detailEntity.自定义8 == null ? "" : detailEntity.自定义8)
                    + "/" + (detailEntity.库存自定义1 == null ? "" : detailEntity.库存自定义1)
                    + "/" + (detailEntity.库存自定义2 == null ? "" : detailEntity.库存自定义2)
                    + "/" + (detailEntity.库存自定义3 == null ? "" : detailEntity.库存自定义3)
                    + "/" + (detailEntity.库存自定义4 == null ? "" : detailEntity.库存自定义4)
                    + "/" + (detailEntity.库存自定义5 == null ? "" : detailEntity.库存自定义5)
                    + "/" + (detailEntity.颜色 == null ? "" : detailEntity.颜色)
            ;
            detailEntity.总规格 = tSpk;

            //在制流转条码 ok
            barcodeEntity.状态 = "OUT";
            barcodeEntity.仓位 = barcodeEntity.仓位;
            barcodeEntity.仓库编号 = barcodeEntity.仓库编号;
            
            //在制流转条码记录 ok
            在制流转条码记录 recEntity = new 在制流转条码记录();
            recEntity.条码 = barcodeEntity.条码;
            if (!"".Equals(paigonganId))
            {
                recEntity.派工单号 = paigonganId;
                recEntity.流程单号 = paigongBeen.流程单号;
            }
            else {
                recEntity.派工单号 = "";
                recEntity.流程单号 ="";
            }
            recEntity.流转时间 = DateTime.Now;
            recEntity.发出工序 = "";
            //派工工序
            if (paigongBeen != null)
            {
                recEntity.收入工序 = paigongBeen.工序系统编号;
            }
            else {
                recEntity.收入工序 = "";
            }
            recEntity.数量 = barcodeEntity.数量;
            recEntity.重量 = barcodeEntity.重量;
            recEntity.单据编号 = no;
            recEntity.创建人 = uid;
            if (!inStorage(headEntity, detailEntity, barcodeEntity, recEntity,paigongBeen)) {
                return "出库失败";
            }
            return "true";
        }

        public bool inStorage(成品出入库主表 headEntity, 成品出入库子表 detailEntity, 在制流转条码 barcodeEntity, 在制流转条码记录 recEntity, 派工单任务 paigongBeen)
        {
            try
            {
               
                db.Ado.BeginTran();
                //新增头
                db.Insertable(headEntity).ExecuteCommand();
                //新增子表
                db.Insertable(detailEntity).ExecuteCommand();
                //更新在制流转条码
                db.Updateable(barcodeEntity).ExecuteCommand();
                //新增在制流转条码记录
                db.Insertable(recEntity).ExecuteCommand();
                if (paigongBeen != null)
                {
                    paigongBeen.领用数量 = paigongBeen.领用数量 + barcodeEntity.数量;
                    //更新派工领用单数量
                    db.Updateable(paigongBeen).ExecuteCommand();
                }
                //库存更新
                //查询是否存在库存记录
                成品总库存 existsSotckEntity = db.Queryable<成品总库存>().Where(q => q.总规格 == detailEntity.总规格).First();
                if (existsSotckEntity == null)
                {
                    //不存在，新增
                    成品总库存 newStockEntity = new 成品总库存();
                    newStockEntity.仓库类别编号 = headEntity.仓库类别编号;
                    newStockEntity.仓库编号 = headEntity.仓库编号;
                    newStockEntity.仓库核算类别 = headEntity.仓库核算类别;
                    newStockEntity.月份 = DateTime.Now.ToString("yyyyMM");
                    newStockEntity.产品系统编号 = detailEntity.产品系统编号;
                    newStockEntity.颜色 = detailEntity.颜色;
                    newStockEntity.自定义1 = detailEntity.自定义1;
                    newStockEntity.自定义2 = detailEntity.自定义2;
                    newStockEntity.自定义3 = detailEntity.自定义3;
                    newStockEntity.自定义4 = detailEntity.自定义4;
                    newStockEntity.自定义5 = detailEntity.自定义5;
                    newStockEntity.自定义6 = detailEntity.自定义6;
                    newStockEntity.自定义7 = detailEntity.自定义7;
                    newStockEntity.自定义8 = detailEntity.自定义8;
                    newStockEntity.库存自定义1 = detailEntity.库存自定义1;
                    newStockEntity.库存自定义2 = detailEntity.库存自定义2;
                    newStockEntity.库存自定义3 = detailEntity.库存自定义3;
                    newStockEntity.库存自定义4 = detailEntity.库存自定义4;
                    newStockEntity.库存自定义5 = detailEntity.库存自定义5;
                    newStockEntity.期初数量 = 0;
                    newStockEntity.入库数量 = 0;
                    newStockEntity.出库数量 = barcodeEntity.数量;
                    newStockEntity.结存数量 = 0- barcodeEntity.数量;
                    newStockEntity.总规格 = detailEntity.总规格;
                    db.Insertable(newStockEntity).ExecuteCommand();
                }
                else
                {
                    //存在，更新
                    existsSotckEntity.出库数量 = existsSotckEntity.出库数量 + barcodeEntity.数量;
                    existsSotckEntity.结存数量 = existsSotckEntity.期初数量+existsSotckEntity.入库数量 - existsSotckEntity.出库数量;
                    db.Updateable(existsSotckEntity).ExecuteCommand();
                }
                //有流程单才会更新
                if(recEntity.派工单号!=null&& !"".Equals(recEntity.派工单号)) { 
                    //更新流程单工序表
                    List<流程单工序明细> liuchengList=db.Queryable<流程单工序明细 > ().Where(q => q.流程单号 == paigongBeen.流程单号).ToList();
                    int paixv=0;
                    foreach (流程单工序明细 liucheng in liuchengList)
                    {
                        if (paigongBeen.工序系统编号.Equals(liucheng.工序系统编号)) {
                            paixv = liucheng.排序;
                            break;
                        }
                    }
                    foreach (流程单工序明细 liucheng in liuchengList)
                    {
                        if (liucheng.排序 < paixv)
                        {
                            liucheng.半成品转入 = liucheng.半成品转入+barcodeEntity.数量;
                            db.Updateable(liucheng).ExecuteCommand();
                        }
                        else if (liucheng.排序 == paixv) {
                            liucheng.收入量 = liucheng.收入量+ barcodeEntity.数量;
                            liucheng.待生产量 = liucheng.收入量 - liucheng.合格产量- liucheng.报废产量;
                            db.Updateable(liucheng).ExecuteCommand();
                        }
                    }
                    //更新流程单表
                    流程单表 liuchengbiao = db.Queryable<流程单表>().Where(q => q.流程单号 == paigongBeen.流程单号).First();
                    liuchengbiao.投料数= liuchengbiao.投料数+ barcodeEntity.数量;
                    db.Updateable(liuchengbiao).ExecuteCommand();
                }
                db.Ado.CommitTran();
                return true;
            }
            catch (Exception ex)
            {
                db.Ado.RollbackTran();
                return false;
            }
        }

        public string getNextSeq(string startWith)
        {
            string ym = DateTime.Now.ToString("yyMM");
            startWith = startWith + ym;
           
            List<成品出入库主表> newList = db.Ado.SqlQuery<成品出入库主表>(" SELECT top 1 * FROM [成品出入库主表] where 单据编号 like @search order by 单据编号 desc", new { search = startWith+"%" });;
            成品出入库主表 newT=null;
            if (newList != null && newList.Count > 0)
            {
                newT=newList[0];
            }
            string sNext;
            if (newT == null)
            {
                sNext = startWith + "0001";
            }
            else
            {
                sNext = startWith + StringHelper.GetNext(newT.单据编号, 4);
            }
            return sNext;
        }
        public PaiGongdan SearchByDingdan(string dinganhao)
        {
            List<PaiGongdan> paiGongdanList = db.Ado.SqlQuery<PaiGongdan>("SELECT t1.[派工单号] as id,t1.[数量] as allCount,t1.[领用数量] as daipaigongCount,t3.[产品名称] as cpName,t3.[规格型号] as cpXinghao FROM[HongSheng].[dbo].[派工单任务] as t1,[HongSheng].[dbo].[流程单表] as t2,[HongSheng].[dbo].[产品资料] as t3 where t1.流程单号=t2.流程单号 and t2.产品系统编号=t3.产品系统编号 and t1.派工单号=@id", new { id = dinganhao });
            if (paiGongdanList != null && paiGongdanList.Count > 0) {
                return paiGongdanList[0];
            }
            else
            {
                return null;
            }
           
        }

        public String SearchChanpinByDingdan(string dinganhao) {
            List<String> paiGongdanList = db.Ado.SqlQuery<String>("SELECT t2.[产品系统编号]  FROM[HongSheng].[dbo].[派工单任务] as t1,[HongSheng].[dbo].[流程单表] as t2,[HongSheng].[dbo].[产品资料] as t3 where t1.流程单号=t2.流程单号 and t2.产品系统编号=t3.产品系统编号 and t1.派工单号=@id", new { id = dinganhao });
            if (paiGongdanList != null && paiGongdanList.Count > 0)
            {
                return paiGongdanList[0];
            }
            else
            {
                return null;
            }
        }
        //根据仓库编号查询仓库数据
        public 标准仓库参数子表 searchCangkuBybianhao(String changkubianhao) {
            List<标准仓库参数子表> entityList = db.Ado.SqlQuery<标准仓库参数子表>("SELECT * FROM 标准仓库参数子表 where 仓库编号 = @cangku", new { cangku = changkubianhao });
            if (entityList != null && entityList.Count > 0)
            {
                return entityList[0];
            }
            else
            {
                return null;
            }
        }
       public 派工单任务 searchByPaiGong(string paigongdanhao)
        {
            List<派工单任务> paiGongdanList = db.Ado.SqlQuery<派工单任务>("SELECT * FROM[HongSheng].[dbo].[派工单任务] where 派工单号 = @id", new { id = paigongdanhao });
            if (paiGongdanList != null && paiGongdanList.Count > 0)
            {
                return paiGongdanList[0];
            }
            else
            {
                return null;
            }

        }
        //查询条码是否正确
        public 在制流转条码 SearchByBarCode(string barcode)
        {
            List<在制流转条码> entityList = db.Ado.SqlQuery<在制流转条码>("SELECT * FROM 在制流转条码 where 条码 = @id and 标签类别 = '合格' and 状态 = 'IN'", new { id = barcode });
            if (entityList != null && entityList.Count > 0)
            {
                return entityList[0];
            }
            else
            {
                return null;
            }
        }
        //获取
        public BarCode searchChanpin(String barcode) {
            List<BarCode> entityList = db.Ado.SqlQuery<BarCode>("SELECT t1.数量 as shulian , t2.产品名称 as chanpinname,t2.规格型号 as xinghao,t1.条码 as tiaoma FROM 在制流转条码 as t1,产品资料 as t2 where 条码=@tiaomaid and t1.产品系统编号=t2.产品系统编号",new { tiaomaid = barcode });

            if (entityList != null && entityList.Count > 0)
            {
                return entityList[0];
            }
            else
            {
                return null;
            }
        }
    }
}
